package com.iweb.day11;

import oracle.jdbc.OracleTypes;

import java.sql.*;

/**
 * @author WuYongheng
 * @date 2021/11/28
 * @description 测试与调用带参数的
 */
public class Test6 {
    public static void main(String[] args) {
        Connection conn = null;
        CallableStatement statement = null;
        ResultSet resultSet = null;

        // 数据库连接信息
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
        String username = "hr";
        String password = "123456";
        try {
            // 加载驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            // 连接对象
            conn = DriverManager.getConnection(url, username, password);
            if (conn != null) {
                System.out.println("连接成功");
            }
            statement = conn.prepareCall("{?=call fun_getFullStus(?)}");
            // 替换?占位符
            // 定义返回值,是游标
            statement.registerOutParameter(1, OracleTypes.CURSOR);
            statement.setString(2, "杨过");
            // 执行
            statement.execute();
            resultSet = (ResultSet) statement.getObject(1);

            while (resultSet.next()) {
                System.out.println(resultSet.getInt("studentno"));
                System.out.println(resultSet.getString("studentname"));
                // todo: 如果学生的出生日期, 要保存到实体类对象的属性中, 怎么操作
                System.out.println(resultSet.getString("borndate"));
                System.out.println();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
